package divinity.objects
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import divinity.basic.*;
	import divinity.events.WorldEvent;
	import divinity.utils.Calculator;
	
	public class WorldObject extends DivinityObject
	{
		private var _vx:Number=0;
		private var _vy:Number=0;
		//private var _gravity:Number=0;
		private var _xspeed:Number=0;
		private var _yspeed:Number=0;
		private var _mass:Number=0;
		private var _faction:int=0;
		private var _health:int=1;
		private var _world:*;
		protected var _elapsed:int;
		protected var _img:*=new Sprite();
		private var _alive:Boolean=true;
		protected var calculator:Calculator=new Calculator();
		public function WorldObject()
		{
			super();
			addChild(_img);
		}
		override public function init(e:Event)
		{
			
			super.init(e);
			
		}
		protected function cycle(e:WorldEvent)
		{
			
			if(_alive)
			{
				_elapsed=e.elapsed;
				moving();
				rotate();
				collision();
				makeMove();
			}
			else
			{
				dead();
			}
			
		}
		protected function moving():void
		{
			
		}
		protected function rotate():void
		{
			
		}
		protected function collision():void
		{
			
		}
		protected function makeMove():void
		{
			
		}	
		protected function dead()
		{			
			parent.removeChild(this);
		}
		override public function destroy(e:Event)
		{
			_world.removeEventListener(WorldEvent.CYCLE,cycle)
			super.destroy(e);
			
		}
		/**
		 * 
		 * 
		 * 
		 * 
		 * 
		 * */
		public function set vx(vectorx:Number):void
		{
			_vx=vectorx;
		}
		public function get vx():Number
		{
			return _vx;
		}
		public function set vy(vectory:Number):void
		{
			_vy=vectory;
		}
		public function get vy():Number
		{
			return _vy;
		}
		/*public function set greavity(grav:Number):void
		{
			_gravity=grav
		}
		public function get gravity():Number
		{
			return _gravity;
		}*/
		public function set xspeed(spd:Number):void
		{
			_xspeed=spd/1000;
		}
		public function get xspeed():Number
		{
			return _xspeed;
		}
		public function set yspeed(spd:Number):void
		{
			_yspeed=spd/1000;
		}
		public function get yspeed():Number
		{
			return _yspeed;
		}
		public function set speed(spd:Number):void
		{
			_xspeed=spd/1000;
			_yspeed=spd/1000;
		}
		public function set mass(ms:Number):void
		{
			_mass=ms;
		}
		public function get mass():Number
		{
			return _mass;
		}
		public function set faction(fac:int):void
		{
			_faction=fac
		}
		public function get faction():int
		{
			return _faction;
		}
		public function set health(hlt:Number):void
		{
			_health=hlt;
		}
		public function get health():Number
		{
			return _health;
		}
		public function set world(wld:*):void
		{
			_world=wld;			
			_world.log=this+" became depended of "+_world+" world";
			_world.addEventListener(WorldEvent.CYCLE,cycle)
		}
		public function get world():DivinityWorld
		{
			return _world;
		}
		/**
		 * 
		 * 	Sets an image for the object. 
		 * 
		 * */
		public function set img(objectImage:*):void
		{
			removeChild(_img);
			_img=objectImage;
			_img.cacheAsbitmap=true;
			addChild(_img);
		}
		public function get img():*
		{
			return _img;
		}
		public function get alive():Boolean
		{
			return _alive;
		}
		public function set alive(alv:Boolean)
		{
			
			_alive=alv;	
		}
	}
}